package com.example.artwindow.handler;

import com.example.artwindow.constants.HttpCodeEnum;
import com.example.artwindow.exception.UnauthorizedException;
import com.example.artwindow.result.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PrintWriter;

@Component
public class AccessDeniedHandler implements AuthenticationEntryPoint {
	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

		response.setHeader("Content-Type","application/json;charset=utf-8");

		PrintWriter writer = response.getWriter();
		writer.write(new ObjectMapper().writeValueAsString(Result.error("认证过期",403)));
		writer.flush();
		writer.close();
	}
}
